@{
    Validation.Exclude("content","summary");
    // EDIT AN ARTICLE
    var view = "~/Views/Admin/_EditArticleView.cshtml";
    // GET ARTICLE ID
    var articleId = UrlData[0];
    // IF NO ARTICLE ID , REDIRECT
    if(articleId.IsEmpty()){Response.Redirect(@Href("~/Actions/Admin/ListAllArticles")); }
    // SET TEMP DEFAUT VALUES
    // TODO implement   tag and author system

    dynamic authorId = WebSecurity.CurrentUserId;
    string title ,summary ,content,published,id,categoryId,enableComments,showComments,creationDate;
    // GET Article by id and author
    dynamic Article = Blog.DataObject.GetArticleByAuthorAndId(articleId,authorId.ToString());
    // if there is no article redirect to home
    if(String.IsNullOrEmpty(Article.id.ToString())){Response.Redirect(@Href("~/",new{errorMessage="Error, no article to edit"}));}
    // GET categories
    var Categories = Blog.DataObject.GetCategories();
    if(IsPost){
    // GET FORM INPUT
    categoryId= Request.Form["categoryId"];
    articleId = Request.Form["articleId"];
    published=Request.Form["published"];
    title = Request.Form["title"];
    summary = Request.Form["summary"];
    content = Request.Form["content"];
    creationDate = Request.Form["creationDate"];
    enableComments = (String.IsNullOrEmpty(Request.Form["enableComments"]))?"0":Request.Form["enableComments"];
    showComments = (String.IsNullOrEmpty(Request.Form["showComments"]))?"0":Request.Form["showComments"];
    // CHECK IF FORM IS VALID
        if((categoryId.IsEmpty())|| (!categoryId.IsInt())){ModelState.AddFormError("Choose a category.");}
        if(title.IsEmpty()){ ModelState.AddFormError("Title cannot be empty");}
        if(published.IsEmpty()){published="0";}
        if(summary.IsEmpty() && content.IsEmpty()){ModelState.AddFormError("Provide at least a summary or a content");} 
        if(!enableComments.IsInt() && !String.IsNullOrEmpty(enableComments)){ModelState.AddFormError("enable comments invalid");};
        if(!showComments.IsInt()&& !String.IsNullOrEmpty(showComments) ){ModelState.AddFormError("show comments invalid");};
        if(!creationDate.IsDateTime()){ModelState.AddFormError("Date of creation is not a valid formated date");};
        // UDPATE ARTICLE
        if(ModelState.IsValid){
            // form is valid , try to insert an article in the database
            var query=Blog.DataObject.EditArticle(title,summary,content,categoryId,authorId.ToString(),
                articleId,published,enableComments,showComments,creationDate);
            if(query>0){
            // set the return view
            view = "~/Actions/Admin/ListAllArticles";
            Response.Redirect(@Href(view,new{successMessage="Article Edited"}));
            }else{
                ModelState.AddFormError("Query Failed!");
            }
          }
    }
    Page.Errors = Html.ValidationSummary() ;   

@RenderPage(view,new{Categories=Categories,Article=Article});
}

